/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.sel1602.csp.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *数据库工具类
 * @author lenovo
 */
public class BaseDao {

    private static final String URL="jdbc:mysql://localhost:3306/cherish?zeroDateTimeBehavior=convertToNull";
    private static final String USER="root";
    private static final String PWD="123456";
    public static Connection getConnection(){
        Connection con =null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection(URL, USER, PWD);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(BaseDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(BaseDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }
    public static void closeConnection(Connection con){
        if(con!=null)
            try {
                con.close();
        } catch (SQLException ex) {
            Logger.getLogger(BaseDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /*public ResultSet executeQuery(String sql, Object... obj) throws SQLException {
        Connection con=BaseDao.getConnection();
        PreparedStatement ps=con.prepareStatement(sql);
        for(int i=0;i<obj.length;i++)
        {
            ps.setObject(i+1, obj[i]);
        }
        ResultSet rs=ps.executeQuery();
        return rs;
    }
    public int execteUpdate (String sql, Object... obj) throws SQLException
    {
        Connection con=BaseDao.getConnection();
        PreparedStatement ps=con.prepareStatement(sql);
        for(int i=0;i<obj.length;i++)
        {
            ps.setObject(i+1, obj[i]);
        }
        int count=ps.executeUpdate();
        return count;
    }
    public int execteAdd (String sql, Object... obj) throws SQLException
    {
        Connection con=BaseDao.getConnection();
        PreparedStatement ps=con.prepareStatement(sql);
        ps.executeUpdate();
        for(int i=0;i<obj.length;i++)
        {
            ps.setObject(i+1, obj[i]);
        }
        int count=ps.executeUpdate();
        return count;
    }
    public int execteDelete (String sql, Object... obj) throws SQLException
    {
        Connection con=BaseDao.getConnection();
        PreparedStatement ps=con.prepareStatement(sql);
        ps.executeUpdate();
        for(int i=0;i<obj.length;i++)
        {
            ps.setObject(i+1, obj[i]);
        }
        int count=ps.executeUpdate();
        return count;
    }*/
}
